<?php

namespace App\Imports;

use App\Models\Good;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class GoodsImport implements ToCollection
{
    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {
        $data = $collection->toArray();
        unset($data['0']);
        if (count($data) > 1) {
            $this->createData($data);
        }
    }

    // 具体导入业务代码
    public function createData($rows)
    {
        foreach ($rows as $key => $value) {
            $model       = new Good();
            $model->name = $value[0];
            $model->save();
        }
    }
}
